/*
 * camera_process.c
 *
 *  Created on: 2023.2.21
 *      Author: Lw
 */

#include "camera_process.h"
#include "xuartlite.h"
#include "dri_hal_uart_lite.h"
#include "dri_hal_can.h"
#include "pl_comm.h"
#include "camera_parameter.h"
#include "sleep.h"
#include "tele_data_transfer.h"
#include "gnc_addit.h"

extern XUartLite UartLiteInst0;
extern XUartLite UartLiteInst1;
extern S_tEquipmentStatus g_tEquipmentStatus;		/* 设备的状态信息结构体 */
static u8 CommandReturnState = XST_FAILURE;
extern S_tAdditData s_Addit1Data;
extern S_tAdditData s_Addit2Data;

#define COMMAND_DELAYTIME     500 //单位ms 在发送开始拍照指令时刻前200ms发送SET IDLE指令
void CameraGain(u8 port);
void CameraGainEx(u8 port);
void CameraOffsetR(u8 port);
void CameraClSetting(u8 port);
void CameraExposureTime(u8 port);
/*
* CMA-1、CMA-2上电初始化参数功能，初始化参数项有：
* 1. 单拍模式
* 2. 曝光时间1000ms；
* 3. 增益为8；
* 4. 图像位数16bit；
* 5. 正常输出Medium模式；
* 6. OFFSET为0；
* 7. 默认拍摄张数1张
* 8. 连拍间隔时间1s
* 初始化完成后断开相机连接，保证处于低功耗。
*/
u8 CameraParaInit(u8 ucPlUartNum)
{
    u32 timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
	/* 422串口指针 */
	XUartLite *pUartLite = NULL;
	g_sCamera1Setting.status = CAMERA_WAIT;
    g_sCamera2Setting.status = CAMERA_WAIT;
    g_sCamera1Setting.commandTrig = CAMERA_WAIT;
    g_sCamera2Setting.commandTrig = CAMERA_WAIT;
    Camera1ShootState = WAIT_SHOOT;
    Camera2ShootState = WAIT_SHOOT;

	/* 1.判断使用的422串口  */
	if(PL_UART_ONE == ucPlUartNum)
	{
	    g_tEquipmentStatus.ucCamera_1_status = CAMERA_CLOSE;
	    g_tEquipmentStatus.ucCamera_1_WorkMode = ORIGIN_IMAGE;
	    g_tEquipmentStatus.uiCamera_1_ExposureTime = 1000000;
        g_tEquipmentStatus.ucCamera_1_GainRGB = 8;
        g_tEquipmentStatus.ucCamera_1_ShootNum = 1;
        g_tEquipmentStatus.ucCamera_1_ShootTime = 2000000;
		pUartLite = &UartLiteInst0;
	}
	else if(PL_UART_TWO == ucPlUartNum)
	{
        g_tEquipmentStatus.ucCamera_2_status = CAMERA_CLOSE;
        g_tEquipmentStatus.ucCamera_2_WorkMode = ORIGIN_IMAGE;
        g_tEquipmentStatus.uiCamera_2_ExposureTime = 1000000;
        g_tEquipmentStatus.ucCamera_2_GainRGB = 8;
        g_tEquipmentStatus.ucCamera_2_ShootNum = 1;
        g_tEquipmentStatus.ucCamera_2_ShootTime = 2000000;
		pUartLite = &UartLiteInst1;
	}
    SetFpgaWorkMode(1, 2);//配置图像流为原图下传工作模式
    SetFpgaPicNum(ucPlUartNum, 1);//默认拍摄张数1张
    SetFpgaShotTimeLapse(ucPlUartNum, 2000000);//设置连拍间隔时间2s

    g_sCamera1Setting.ucExposureTime[3] = 0x00;
    g_sCamera1Setting.ucExposureTime[2] = 0x0F;
    g_sCamera1Setting.ucExposureTime[1] = 0x42;
    g_sCamera1Setting.ucExposureTime[0] = 0x40;
    g_sCamera2Setting.ucExposureTime[3] = 0x00;
    g_sCamera2Setting.ucExposureTime[2] = 0x0F;
    g_sCamera2Setting.ucExposureTime[1] = 0x42;
    g_sCamera2Setting.ucExposureTime[0] = 0x40;

    g_sCamera1Setting.ucGain[1] = 0x00;
    g_sCamera1Setting.ucGain[0] = 0x08;
    g_sCamera2Setting.ucGain[1] = 0x00;
    g_sCamera2Setting.ucGain[0] = 0x08;

	XUartLite_Send(pUartLite, CONNECT_1, sizeof(CONNECT_1));//进入相机用户程序
	sleep(1);//需延迟1s
	XUartLite_Send(pUartLite, CONNECT_2, sizeof(CONNECT_2));//初始化相机内部寄存器
	sleep(1);//延迟1s

	/*进入低功耗*/
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B901, CAM_MSG_LEN);
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){}
    CommandReturnState = XST_FAILURE;
	XUartLite_Send(pUartLite, B902, CAM_MSG_LEN);
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    XUartLite_Send(pUartLite, DISCONNECT, sizeof(DISCONNECT));

	return CommandReturnState;
}

void CameraProcessUpdate(void)
{
    if(g_sCamera1Setting.status == CAMERA_OPEN){
        OpenCamera(CAMERA_ONE);
        g_tEquipmentStatus.ucCamera_1_status = CAMERA_OPEN;
        g_sCamera1Setting.status = CAMERA_WAIT;
    }
    else if(g_sCamera1Setting.status == CAMERA_CLOSE){
        CloseCamera(CAMERA_ONE);
        g_tEquipmentStatus.ucCamera_1_status = CAMERA_CLOSE;
        g_sCamera1Setting.status = CAMERA_WAIT;
    }

    if(g_sCamera2Setting.status == CAMERA_OPEN){
        OpenCamera(CAMERA_TWO);
        g_tEquipmentStatus.ucCamera_2_status = CAMERA_OPEN;
        g_sCamera2Setting.status = CAMERA_WAIT;
    }
    else if(g_sCamera2Setting.status == CAMERA_CLOSE){
        CloseCamera(CAMERA_TWO);
        g_tEquipmentStatus.ucCamera_2_status = CAMERA_CLOSE;
        g_sCamera2Setting.status = CAMERA_WAIT;
    }

    if(g_sCamera1Setting.commandTrig == A3_COMMAND){
        g_sCamera1Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_ONE, A3_COMMAND);
    }
    else if(g_sCamera1Setting.commandTrig == A4_COMMAND){
        g_sCamera1Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_ONE, A4_COMMAND);
    }
    else if(g_sCamera1Setting.commandTrig == A8_COMMAND){
        g_sCamera1Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_ONE, A8_COMMAND);
    }
    else if(g_sCamera1Setting.commandTrig == B3_COMMAND){
        g_sCamera1Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_ONE, B3_COMMAND);
    }

    if(g_sCamera2Setting.commandTrig == A3_COMMAND){
        g_sCamera2Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_TWO, A3_COMMAND);
    }
    else if(g_sCamera2Setting.commandTrig == A4_COMMAND){
        g_sCamera2Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_TWO, A4_COMMAND);
    }
    else if(g_sCamera2Setting.commandTrig == A8_COMMAND){
        g_sCamera2Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_TWO, A8_COMMAND);
    }
    else if(g_sCamera2Setting.commandTrig == B3_COMMAND){
        g_sCamera2Setting.commandTrig = CAMERA_WAIT;
        CameraCommandSend(CAMERA_TWO, B3_COMMAND);
    }

}


void updataUTCtime(void)
{
    s_tUTCTime.TimeS = ReadSTime();
    s_tUTCTime.TimeMs = ReadMsTime();
}

/*
 * 输入的参数有
 * 拍摄张数 g_tEquipmentStatus.ucCamera_1_ShootNum
 * 曝光时间 g_tEquipmentStatus.uiCamera_1_ExposureTime，
 * 连拍间隔时间 g_tEquipmentStatus.ucCamera_1_ShootTime
 * 拍摄张数 g_tEquipmentStatus.ucCamera_2_ShootNum
 * 曝光时间 g_tEquipmentStatus.uiCamera_2_ExposureTime，
 * 连拍间隔时间 g_tEquipmentStatus.ucCamera_2_ShootTime
 *
 * */
void CameraBurstShootProcess(void)
{

    switch(Camera1ShootState)
    {
        case WAIT_SHOOT:
        break;

        case BEGIN_SHOOT:
        	updataUTCtime();
            g_sCamera1Setting.beginShootTimeS = s_tUTCTime.TimeS;
            g_sCamera1Setting.beginShootTimeMs = s_tUTCTime.TimeMs;
            s_Addit1Data.State = BEGIN_PHOT;
            XUartLite_Send(&UartLiteInst0, B9_RELEASE_IDLE, CAM_MSG_LEN);
            SetFpgaShot(CAMERA_ONE);
            g_sCamera1Setting.shootIntervalTimeMs = (g_tEquipmentStatus.uiCamera_1_ExposureTime+g_tEquipmentStatus.ucCamera_1_ShootTime)/1000;
            Camera1ShootState = BURST_SETIDLE;
            g_sCamera1Setting.shootCount = 1;
            g_sCamera1Setting.shootSetIdleFlag = 1;
        break;

        case BURST_SETIDLE:
            g_sCamera1Setting.shootDuringTimeMs = (s_tUTCTime.TimeS-g_sCamera1Setting.beginShootTimeS-1)*1000+(1000-g_sCamera1Setting.beginShootTimeMs)+s_tUTCTime.TimeMs;
            if(g_sCamera1Setting.shootDuringTimeMs >= (g_sCamera1Setting.shootIntervalTimeMs*g_sCamera1Setting.shootCount-COMMAND_DELAYTIME) && g_sCamera1Setting.shootSetIdleFlag == 1)
            {
                g_sCamera1Setting.shootSetIdleFlag = 0;
                XUartLite_Send(&UartLiteInst0, B9_SET_IDLE, CAM_MSG_LEN);
                if(g_sCamera1Setting.shootCount >= g_tEquipmentStatus.ucCamera_1_ShootNum)
                {
                    Camera1ShootState = WAIT_SHOOT;
                    s_Addit1Data.State = WAIT_PHOT;
                }
            }
            else if(g_sCamera1Setting.shootDuringTimeMs >= g_sCamera1Setting.shootIntervalTimeMs*g_sCamera1Setting.shootCount)
            {
                Camera1ShootState = BURST_RELEASEIDLE;
            }

        break;

        case BURST_RELEASEIDLE:
        	s_Addit1Data.PhotoFlag = 1;
        	updataUTCtime();
        	s_Addit1Data.ShootMomentS = s_tUTCTime.TimeS;
        	s_Addit1Data.ShootMomentMs = s_tUTCTime.TimeMs;
            XUartLite_Send(&UartLiteInst0, B9_RELEASE_IDLE, CAM_MSG_LEN);
            Camera1ShootState = BURST_SETIDLE;
            g_sCamera1Setting.shootCount++;
            g_sCamera1Setting.shootSetIdleFlag = 1;
        break;

        default:
        break;
    }

    switch(Camera2ShootState)
    {
        case WAIT_SHOOT:
        break;

        case BEGIN_SHOOT:
        	updataUTCtime();
            g_sCamera2Setting.beginShootTimeS = s_tUTCTime.TimeS;
            g_sCamera2Setting.beginShootTimeMs = s_tUTCTime.TimeMs;
            s_Addit2Data.State = BEGIN_PHOT;
            XUartLite_Send(&UartLiteInst1, B9_RELEASE_IDLE, CAM_MSG_LEN);
            SetFpgaShot(CAMERA_TWO);
            g_sCamera2Setting.shootIntervalTimeMs = (g_tEquipmentStatus.uiCamera_2_ExposureTime+g_tEquipmentStatus.ucCamera_2_ShootTime)/1000;
            Camera2ShootState = BURST_SETIDLE;
            g_sCamera2Setting.shootCount = 1;
            g_sCamera2Setting.shootSetIdleFlag = 1;
        break;

        case BURST_SETIDLE:
            g_sCamera2Setting.shootDuringTimeMs = (s_tUTCTime.TimeS-g_sCamera2Setting.beginShootTimeS-1)*1000+(1000-g_sCamera2Setting.beginShootTimeMs)+s_tUTCTime.TimeMs;
            if(g_sCamera2Setting.shootDuringTimeMs >= (g_sCamera2Setting.shootIntervalTimeMs*g_sCamera2Setting.shootCount-COMMAND_DELAYTIME)
              && g_sCamera2Setting.shootSetIdleFlag == 1)
            {
                g_sCamera2Setting.shootSetIdleFlag = 0;
                XUartLite_Send(&UartLiteInst1, B9_SET_IDLE, CAM_MSG_LEN);
                if(g_sCamera2Setting.shootCount >= g_tEquipmentStatus.ucCamera_2_ShootNum)
                {
                    Camera2ShootState = WAIT_SHOOT;
                    s_Addit2Data.State = WAIT_PHOT;
                }
            }
            else if(g_sCamera2Setting.shootDuringTimeMs >= g_sCamera2Setting.shootIntervalTimeMs*g_sCamera2Setting.shootCount)
            {
                Camera2ShootState = BURST_RELEASEIDLE;
            }

        break;

        case BURST_RELEASEIDLE:
        	updataUTCtime();
        	s_Addit2Data.PhotoFlag = 1;
        	s_Addit2Data.ShootMomentS = s_tUTCTime.TimeS;
        	s_Addit2Data.ShootMomentMs = s_tUTCTime.TimeMs;
            XUartLite_Send(&UartLiteInst1, B9_RELEASE_IDLE, CAM_MSG_LEN);
            Camera2ShootState = BURST_SETIDLE;
            g_sCamera2Setting.shootCount++;
            g_sCamera2Setting.shootSetIdleFlag = 1;
        break;

        default:
        break;
    }
}
/*打开相机 根据设置的参数来打开相机,打开后进入IDLE模式*/
u8 OpenCamera(u8 ucPlUartNum)
{
    u32 timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    /* 422串口指针 */
    XUartLite *pUartLite = NULL;

    /* 1.判断使用的422串口  */
    if(PL_UART_ONE == ucPlUartNum)
    {
        pUartLite = &UartLiteInst0;
    }
    else if(PL_UART_TWO == ucPlUartNum)
    {
        pUartLite = &UartLiteInst1;
    }

    XUartLite_Send(pUartLite, CONNECT_1, sizeof(CONNECT_1));//进入相机用户程序
    sleep(1);
    XUartLite_Send(pUartLite, CONNECT_2, sizeof(CONNECT_2));//初始化相机内部寄存器
    sleep(1);
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A0, sizeof(A0));//相机初始化 ,视频流模式 ,16bit,正常输出模式
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CameraExposureTime(ucPlUartNum);//曝光时间更新
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A3, sizeof(A3));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CameraGain(ucPlUartNum);//增益更新
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A4, sizeof(A4));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
/*
    CameraOffsetR(ucPlUartNum);
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A8, sizeof(A8));//配置OFFSET
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
*/
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A7, sizeof(A7));//图像位数为16bit
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B3, sizeof(B3));//配置为正常模式输出，Medium
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B910, sizeof(B910));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B911, sizeof(B911));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B912, sizeof(B912));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B913, sizeof(B913));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CameraExposureTime(ucPlUartNum);//曝光时间更新
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A3, sizeof(A3));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CameraGain(ucPlUartNum);//增益更新
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A4, sizeof(A4));
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
/*
    CameraOffsetR(ucPlUartNum);
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, A8, sizeof(A8));//配置OFFSET
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
*/
    /*进入set idle*/
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B9_SET_IDLE, CAM_MSG_LEN);
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    CommandReturnState = XST_FAILURE;
    CameraCommandSend(ucPlUartNum, D504_COMMAND);//读取温度
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--){};

    return XST_SUCCESS;//CommandReturnState;
}

/*关闭相机 使其进入低功耗*/
u8 CloseCamera(u8 ucPlUartNum)
{
    u32 timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    /* 422串口指针 */
    XUartLite *pUartLite = NULL;

    /* 1.判断使用的422串口  */
    if(PL_UART_ONE == ucPlUartNum)
    {
        pUartLite = &UartLiteInst0;
    }
    else if(PL_UART_TWO == ucPlUartNum)
    {
        pUartLite = &UartLiteInst1;
    }
    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B901, CAM_MSG_LEN);
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--);

    CommandReturnState = XST_FAILURE;
    XUartLite_Send(pUartLite, B902, CAM_MSG_LEN);
    timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
    while(CommandReturnState == XST_FAILURE && timeoutCnt--);

    XUartLite_Send(pUartLite, DISCONNECT, sizeof(DISCONNECT));
    return XST_SUCCESS;
}

void CameraCommandSend(u8 ucPlUartNum, u8 ucCommand)
{
    u32  timeoutCnt = U_SECOND_COUNT*1000*1000;
	/* 422串口指针 */
	XUartLite *pUartLite = NULL;
	/* 1.判断使用的422串口  */
	if(PL_UART_ONE == ucPlUartNum){
		pUartLite = &UartLiteInst0;
	}
	else if(PL_UART_TWO == ucPlUartNum){
		pUartLite = &UartLiteInst1;
	}
	switch(ucCommand)
	{
		case D2_COMMAND:
		{
			XUartLite_Send(pUartLite, D2, sizeof(D2));
			break;
		}
		case D504_COMMAND:
		{
			XUartLite_Send(pUartLite, D504, sizeof(D504));
			break;
		}
		case A3_COMMAND:
		{
			CameraExposureTime(ucPlUartNum);
		    CommandReturnState = XST_FAILURE;
            XUartLite_Send(pUartLite, A3, sizeof(A3));
            timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
            while(CommandReturnState == XST_FAILURE && timeoutCnt--){}
		    /*进入set idle*/
		    XUartLite_Send(pUartLite, B9_SET_IDLE, CAM_MSG_LEN);
			break;
		}
		case A4_COMMAND:
		{
			CameraGain(ucPlUartNum);
			CameraGainEx(ucPlUartNum);
		    CommandReturnState = XST_FAILURE;
			XUartLite_Send(pUartLite, A4, sizeof(A4));
            timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
            while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
            /*进入set idle*/
            XUartLite_Send(pUartLite, B9_SET_IDLE, CAM_MSG_LEN);
			break;
		}
		case A8_COMMAND:
		{
			CameraOffsetR(ucPlUartNum);
		    CommandReturnState = XST_FAILURE;
			XUartLite_Send(pUartLite, A8, sizeof(A8));
            timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
            while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
            /*进入set idle*/
            XUartLite_Send(pUartLite, B9_SET_IDLE, CAM_MSG_LEN);
			break;
		}
		case B3_COMMAND:
		{
			CameraClSetting(ucPlUartNum);
		    CommandReturnState = XST_FAILURE;
			XUartLite_Send(pUartLite, B3, sizeof(B3));
            timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
            while(CommandReturnState == XST_FAILURE && timeoutCnt--){};
            /*进入set idle*/
            XUartLite_Send(pUartLite, B9_SET_IDLE, CAM_MSG_LEN);
			break;
		}
		default:
		{
			break;
		}
	}

}
void GetCameraInformation(int iCameraNum, u8 *puacCameraMsg)
{
	//int iIndex = 0;
	int iIsD504Recv = (0x66 == puacCameraMsg[CCI0_INDEX]) && (0x18 == puacCameraMsg[CCI1_INDEX]);
	char acCameraFpgaTemperature[2] = {0};

	if(1 == iIsD504Recv)
	{
		acCameraFpgaTemperature[0] = puacCameraMsg[CCI11_INDEX];
		acCameraFpgaTemperature[1] = puacCameraMsg[CCI12_INDEX];
		if(1 == iCameraNum)
		{
			g_tEquipmentStatus.usCamera_1_CurrentTmp = (693 * (acCameraFpgaTemperature[1] * 256 + acCameraFpgaTemperature[0]) / 1024) - 265;
		}
		else if(2 == iCameraNum)
		{
			g_tEquipmentStatus.usCamera_2_CurrentTmp = (693 * (acCameraFpgaTemperature[1] * 256 + acCameraFpgaTemperature[0]) / 1024) - 265;
		}
	}

}

void CameraCommandRecv(int iCameraNum, u8 *puacCameraMsg)
{
	int iIndex = 0;
	u8 uacCamRecvMsg[CAM_MSG_LEN] = {0};
	CommandReturnState = XST_SUCCESS;
	memcpy(uacCamRecvMsg, puacCameraMsg, CAM_MSG_LEN);

	for(iIndex = 4; iIndex <= 15; iIndex++)
	{
		if(uacCamRecvMsg[iIndex] != RecvSuccess[iIndex])
		{
			CommandReturnState = XST_FAILURE;
		}
	}
	/* 将收到的相机返回值与成功的消息类型进行对比  */
	for(iIndex = 0; iIndex < CAM_MSG_LEN; iIndex++)
	{
		if(uacCamRecvMsg[iIndex] != RecvSuccess[iIndex])
		{
			//iCamMsgStatus = CAM_MSG_FAILED;
			if(16 == iIndex)
			{
				GetCameraInformation(iCameraNum, puacCameraMsg);
				break;
			}
			else if(0xD5 == uacCamRecvMsg[iIndex])
			{
				GetCameraInformation(iCameraNum, puacCameraMsg);
				break;
			}
			else
			{
				return;
			}
		}
	}
}


void CameraExposureTime(u8 port)
{
    if(port == CAMERA_ONE){
        A3[CCP1_INDEX] = g_sCamera1Setting.ucExposureTime[3];
        A3[CCP2_INDEX] = g_sCamera1Setting.ucExposureTime[2];
        A3[CCP3_INDEX] = g_sCamera1Setting.ucExposureTime[1];
        A3[CCP4_INDEX] = g_sCamera1Setting.ucExposureTime[0];
    }
    else if(port == CAMERA_TWO){
        A3[CCP1_INDEX] = g_sCamera2Setting.ucExposureTime[3];
        A3[CCP2_INDEX] = g_sCamera2Setting.ucExposureTime[2];
        A3[CCP3_INDEX] = g_sCamera2Setting.ucExposureTime[1];
        A3[CCP4_INDEX] = g_sCamera2Setting.ucExposureTime[0];
    }
}

/*************************************************************************
* 函数名称：	CameraGain
* 功能：		相
**************************************************************************
* 输入变量：		无
* 输出变量：		无
* 返回值：		无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.11.07           张海峰          		创建
* xxxx.xx.xx    XXX          添加/修改了XXXXXXXXXXX
*************************************************************************/
void CameraGain(u8 port)
{
    if(port == CAMERA_ONE){
        A4[CCP1_INDEX] = g_sCamera1Setting.ucAnaGain[1];
        A4[CCP2_INDEX] = g_sCamera1Setting.ucAnaGain[0];

        A4[CCP3_INDEX] = g_sCamera1Setting.ucGain[1];
        A4[CCP4_INDEX] = g_sCamera1Setting.ucGain[0];

    }
    else if(port == CAMERA_TWO){
        A4[CCP1_INDEX] = g_sCamera2Setting.ucAnaGain[1];
        A4[CCP2_INDEX] = g_sCamera2Setting.ucAnaGain[0];

        A4[CCP3_INDEX] = g_sCamera2Setting.ucGain[1];
        A4[CCP4_INDEX] = g_sCamera2Setting.ucGain[0];

    }
}
void CameraGainEx(u8 port)
{
    if(port == CAMERA_ONE){
        A4[CCP13_INDEX] = g_sCamera1Setting.ucGainExtend[1];
        A4[CCP14_INDEX] = g_sCamera1Setting.ucGainExtend[0];
        A4[CCP15_INDEX] = g_sCamera1Setting.ucGainHLGC[1];
        A4[CCP16_INDEX] = g_sCamera1Setting.ucGainHLGC[0];
    }
    else if(port == CAMERA_TWO){
        A4[CCP13_INDEX] = g_sCamera2Setting.ucGainExtend[1];
        A4[CCP14_INDEX] = g_sCamera2Setting.ucGainExtend[0];
        A4[CCP15_INDEX] = g_sCamera2Setting.ucGainHLGC[1];
        A4[CCP16_INDEX] = g_sCamera2Setting.ucGainHLGC[0];
    }

}
void CameraOffsetR(u8 port)
{
    if(port == CAMERA_ONE){
        A8[CCP1_INDEX] = g_sCamera1Setting.ucOffsetR[1];
        A8[CCP2_INDEX] = g_sCamera1Setting.ucOffsetR[0];
        A8[CCP7_INDEX] = g_sCamera1Setting.ucOffsetR[3];
        A8[CCP8_INDEX] = g_sCamera1Setting.ucOffsetR[2];
    }
    else if(port == CAMERA_TWO){
        A8[CCP1_INDEX] = g_sCamera2Setting.ucOffsetR[1];
        A8[CCP2_INDEX] = g_sCamera2Setting.ucOffsetR[0];
        A8[CCP7_INDEX] = g_sCamera2Setting.ucOffsetR[3];
        A8[CCP8_INDEX] = g_sCamera2Setting.ucOffsetR[2];
    }
}
void CameraClSetting(u8 port)
{
    if(port == CAMERA_ONE){
        B3[CCP1_INDEX] = g_sCamera1Setting.ucClPicSet;
    }
    else if(port == CAMERA_TWO){
        B3[CCP1_INDEX] = g_sCamera2Setting.ucClPicSet;
    }
}

/*************************************************************************
* 函数名称：	CameraDataRecv
* 功能：		相机数据返回值解析
**************************************************************************
* 输入变量：		无
* 输出变量：		无
* 返回值：		无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.11.07           张海峰          		创建
* xxxx.xx.xx    XXX          添加/修改了XXXXXXXXXXX
*************************************************************************/
void CameraDataRecv(u8 *puacCameraMsg)
{
	int iIndex = 0;

	u8 uacCamRecvMsg[CAM_MSG_LEN] = {0};
	memcpy(uacCamRecvMsg, puacCameraMsg, CAM_MSG_LEN);

	g_sCameraStatus.ucSpeedSet = uacCamRecvMsg[CCI0_INDEX];
	g_sCameraStatus.ucCurrentTempMode = uacCamRecvMsg[CCI12_INDEX];

	g_sCameraStatus.uacCurrentTemp[1] = uacCamRecvMsg[CCI13_INDEX];
	g_sCameraStatus.uacCurrentTemp[0] = uacCamRecvMsg[CCI14_INDEX];

	g_sCameraStatus.uacTargetTemp[1] = uacCamRecvMsg[CCI15_INDEX];
	g_sCameraStatus.uacTargetTemp[0] = uacCamRecvMsg[CCI16_INDEX];

	g_sCameraStatus.ucCurrentPWM = uacCamRecvMsg[CCI17_INDEX];
	g_sCameraStatus.ucCurrentTempMode = uacCamRecvMsg[CCI18_INDEX];

	g_sCameraStatus.uacCmosXSize[1] = uacCamRecvMsg[CCI28_INDEX];
	g_sCameraStatus.uacCmosXSize[0] = uacCamRecvMsg[CCI29_INDEX];

	g_sCameraStatus.uacCmosYSize[1] = uacCamRecvMsg[CCI30_INDEX];
	g_sCameraStatus.uacCmosYSize[0] = uacCamRecvMsg[CCI31_INDEX];

	g_sCameraStatus.ucPicBit = uacCamRecvMsg[CCI32_INDEX];
	g_sCameraStatus.ucCamModel = uacCamRecvMsg[CCI46_INDEX];

	g_sCameraStatus.ucCamColor = uacCamRecvMsg[CCI47_INDEX];

	for(; iIndex < 16; iIndex++)
	{
		g_sCameraStatus.ucCamSerialNum[iIndex] = uacCamRecvMsg[CCI47_INDEX + iIndex];
	}
}





